/**
 * Copyright(c) kyle.
 */
package org.example.tomcat.netty.basic;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

import java.nio.charset.StandardCharsets;

/**
 * 响应
 *
 * @author kyle
 * @version 1.00.00
 * @since [2020-10-20 19:50]
 * @updateUser [Kyle]
 * @updateRemark [说明本次修改内容]
 * @date [2020-10-20 19:50]
 */
public class GPResponse {
    private final ChannelHandlerContext ctx;
    private final HttpRequest req;

    public GPResponse(final ChannelHandlerContext pCtx, final HttpRequest pReq) {
        ctx = pCtx;
        req = pReq;
    }

    public void write(String out) throws Exception {
        try {
            if (out == null || out.length() == 0) {
                return;
            }
            FullHttpResponse lvResponse = new DefaultFullHttpResponse(
                    HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    Unpooled.wrappedBuffer(out.getBytes(StandardCharsets.UTF_8)));
            lvResponse.headers().set("Content-Type", "text/html;");
//写入响应
            ctx.write(lvResponse);
        } finally {
            ctx.flush();
            ctx.close();
        }
    }
}
